package repositories;

import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.ArrayList;

import specifications.CitySpecification;
import dbHelper.DBHelper;
import entities.City;

public class CityRepository  implements Repository<City>{
	
	
	public ArrayList<City> CityList;
	
	
	public CityRepository()
	{
		this.CityList = null;
	}

	@Override
	public void Add(City City) {
		try {
			DBHelper.addObject(City);
		} catch (NoSuchMethodException | IllegalAccessException
				| IllegalArgumentException | InvocationTargetException
				| SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	@Override
	public void Delete(City City) {
		try {
			DBHelper.deleteObject(City);
		} catch (IllegalAccessException | IllegalArgumentException
				| InvocationTargetException | NoSuchMethodException
				| SecurityException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	@Override
	public void Update(City City) {
		try {
			DBHelper.updateObject(City);
		} catch (NoSuchMethodException | IllegalAccessException
				| IllegalArgumentException | InvocationTargetException
				| SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	@Override
	public void FindAll() {
		City C = new City();
		try {
			this.CityList = (ArrayList<City>) DBHelper.findObjects(C);
		} catch (IllegalAccessException | IllegalArgumentException
				| InvocationTargetException | InstantiationException
				| NoSuchMethodException | SecurityException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public ArrayList<City> findBySpecification (CitySpecification spec) {
		if(this.CityList == null)
			this.FindAll();
		ArrayList<City> res = new ArrayList<City>();
		for (City el : CityList)
		{
			if (spec.isSatisfiedBy(el))
			{
				res.add(el);
			}
		}
		return res;
	}


}
